package io.renren.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import io.renren.entity.SysUserEntity;
import io.renren.utils.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.entity.SocialsbSummaryEntity;
import io.renren.service.SocialsbSummaryService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;


/**
 * 社保付款管理
 * 
 * @author zhangheng
 * @email zh_123shmily@163.com
 * @date 2017-07-21 17:22:48
 */
@RestController
@RequestMapping("socialsbsummary")
public class SocialsbSummaryController extends AbstractController{
	@Autowired
	private SocialsbSummaryService socialsbSummaryService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("socialsbsummary:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<SocialsbSummaryEntity> socialsbSummaryList = socialsbSummaryService.queryList(query);
		int total = socialsbSummaryService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(socialsbSummaryList, total, query.getLimit(), query.getPage());
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			int tempmonth=0,tempyear=0;
			if(params.get("batchcode")!=null&&!"".equals(params.get("batchcode").toString())){
				String batchcode = params.get("batchcode").toString();
				String[] str = batchcode.split("_");
				if(str.length!=3){
					throw new RRException("填写的批次号不规范!");
				}
				if(str[1]!=null&&"01".equals(str[1])){
					tempyear = Integer.parseInt(str[0])-1;
					tempmonth = 12;
				}else{
					tempyear = Integer.parseInt(str[0]);
					tempmonth = Integer.parseInt(str[1])-1;
				}
				map =  socialsbSummaryService.getMapEntity(str[0],str[1],str[2],tempyear,tempmonth);
			}
		}catch (RRException e) {
			//logger.info("e",e);
			throw e;
		}

		return R.ok().put("page", pageUtil).put("map",map);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("socialsbsummary:info")
	public R info(@PathVariable("id") Integer id){
		SocialsbSummaryEntity socialsbSummary = socialsbSummaryService.queryObject(id);
		
		return R.ok().put("socialsbSummary", socialsbSummary);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("socialsbsummary:save")
	public R save(@RequestBody SocialsbSummaryEntity socialsbSummary){
		socialsbSummaryService.save(socialsbSummary);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("socialsbsummary:update")
	public R update(@RequestBody SocialsbSummaryEntity socialsbSummary){
		socialsbSummaryService.update(socialsbSummary);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("socialsbsummary:delete")
	public R delete(@RequestBody Integer[] ids){
		socialsbSummaryService.deleteBatch(ids);
		
		return R.ok();
	}
	//插入社保付款
	@RequestMapping("/insertsocial")
	public R insertSocial(@RequestBody JSONObject params) {
		Map<String,Object> map = socialsbSummaryService.insertSocial(params,getUser());

		return R.ok().put("map",map);
	}
	/**
	 * 审核
	 */
	@RequestMapping("/simpleaudit")
	public R simpleAudit(@RequestBody JSONObject params){
		socialsbSummaryService.simpleAudit(params,getUser());
		return R.ok();
	}
}
